package br.uniminas.persistencia;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import br.uniminas.entidades.Departamento;


public class DepartamentoDao {
	private List<Departamento> depList;
	private Session session = HibernateUtil.getSessionFactory().getCurrentSession();

	public List getAllDepartments() {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			depList = session.createQuery("from Departamento").list();
			return depList;
		} catch (HibernateException e) {
			throw e;
		} finally {
			//session.close();
		}
	}

	public static void main(String[] args) {
		DepartamentoDao depDao = new DepartamentoDao();
		List<Departamento> deps = depDao.getAllDepartments();
		for(Iterator<Departamento> i=deps.iterator();i.hasNext();){
			System.out.println(i.next().getNome());
		}
	}

	public void insert(br.uniminas.entidades.Departamento dept) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.save(dept);
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		}		
	}
}
